<template>
  <div class="student-dorm-min">
    <!-- 搜索栏 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="12">
          <el-input v-model="search.name" placeholder="宿舍名称" class="dormsearch"></el-input>
        </el-col>
        <el-col :span="12" style="text-align:right">
          <el-button type="primary" @click="doSearch" size="small">搜索</el-button>
          <el-button type="primary" plain @click="doReset" size="small">重置</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 列表展示 -->
    <el-card>
      <div slot="header">
        <span>表格数据</span>
        <el-button type="primary" size="small" style="float:right" @click="addInfo">添加宿舍信息</el-button>
      </div>
      <el-table :data="dormList" border style="width: 100%" v-loading="loading">
        <el-table-column fixed label="#" width="50" type="index"></el-table-column>
        <el-table-column prop="name" label="宿舍名称" width="150"></el-table-column>
        <el-table-column prop="type" label="宿舍类型" width="120"></el-table-column>
        <el-table-column prop="position" label="宿舍地址" width="120"></el-table-column>
        <el-table-column prop="bak" label="宿舍描述" width="120"></el-table-column>
        <el-table-column prop="startTime" label="开始时间" width="220"></el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="220"></el-table-column>
        <el-table-column prop="addTime" label="添加时间" width="220"></el-table-column>
        <el-table-column fixed="right" label="操作" width="130">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
            <el-button
              type="text"
              size="small"
              style="color:orange"
              @click="doEdit(scope.row.id)"
            >编辑</el-button>
            <el-button type="text" size="small" style="color:red" @click="doDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align:right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.start"
        :page-sizes="page.sizes"
        :page-size="page.limit"
        :total="page.totalcount"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </el-card>
    <Detail ref="detail" @refresh-list="getdormList" />
  </div>
</template>

<script>
import { dormList, dormDetail, dormDel } from "@/api/system/dorm";
import Detail from "./Detail";
import _ from "lodash";
export default {
  components: {
    Detail,
  },
  data() {
    return {
      search: {},
      dormList: [],
      page: {
        limit: 10,
        start: 1,
        sizes: [10, 20, 30],
        totalcount: 0,
      },
      loading: false,
    };
  },
  created() {
    this.getdormList();
  },
  methods: {
    doSearch() {
      this.getdormList();
      this.page.start = 1;
    },
    doReset() {
      this.search = {};
    },
    handleClick() {},
    getdormList() {
      this.loading = true;
      var params = {
        start: this.page.start,
        limit: this.page.limit,
        ...this.search,
      };
      params = _.pickBy(params, (item) => item); //去掉查询时候的冗余信息
      _.pickBy(params, function (v) {
        return v;
      });
      dormList(params).then((res) => {
        this.dormList = res.data.list;
        this.page.totalcount = res.data.totalRow;
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page.limit = val;
      this.getdormList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.start = val;
      this.getdormList();
    },
    addInfo() {
      this.$refs.detail.openDialog();
    },
    doEdit(id) {
      this.$refs.detail.openDialogEdit(id);
    },
    doDel(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          dormDel({ id }).then((res) => {
            var index = this.dormList.findIndex((item) => {
              return item.id == id;
            });
            this.dormList.splice(index, 1);
            this.getdormList();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang='scss' scoped>
.dormsearch {
  width: 220px;
}
.box-card {
  margin: 30px;
}
</style>